function Code() {
	this.str = "";
	this.i=$('#yzm_a').children('i');
	this.arr=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
}
Code.prototype.init = function() {
	var that = this;
	this.str = "";
	this.i.each(function() {
		var color = "#";
		for(var i = 0; i < 6; i++) {
			color += that.arr[Math.floor(Math.random() * 16)];
		}
		$(this).css('color', color);
		var text = Math.floor(Math.random() * 10);
		$(this).html(text);
		that.str += text;
	});
}
Code.prototype.changeCode = function() {
	var that = this;
	this.i.on('click', function() {
		that.init();
	})
}
$(function() {
	var code = new Code();
	var test = new Test();
	var input = $('input[name=checkcode]');
	var div=$('.danger');
	code.init();
	code.changeCode();
	$('.login_a').on('click', function() {
		if($.trim(input.val()) === code.str) {
			test.checkUser($('input[name=username]'));
			if(test.flag) {
				$.ajax({
					type: 'POST',
					url: 'php/login.php',
					data: $.param({
						user: $('input[name=username]').val(),
						pw: $('input[name=password]').val()
					}),
					success: function(response, status, xhr) {
						if(response == 1) {
							alert("登录成功");
							$.cookie('username', $('input[name=username]').val(), { expires: 7 });
							window.open("index.html", "_parent");
						} else {
							div.html(response);
							code.init();
							input.val('');
						}
					}
				})
			} else {
				div.html("用户名输入不合法");
			}
		} else {
			div.html("验证码输入有误");
			//			input.val('');
			//			input.focus();
			input.keydown(function() {
				$('.danger').html("");
			});
		}
	});
	$('input').focus(function() {
		div.html("");
	})
   document.addEventListener('plusready', function() {
		//console.log("所有plus api都应该在此事件发生后调用，否则会出现plus is undefined。"
		plus.key.addEventListener('backbutton', function() {
			//					plus.runtime.quit();
				history.back();
		});
	});
})